package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.vo.CategoryVO;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    @DubboReference
    private ItemCatService itemCatService;

    @DubboReference
    private ItemService itemService;


    //跳转商品审核页面
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }

    //查询商品表
    @RequestMapping("goodsInfo")
    @ResponseBody
    public AjaxResult getGoodsInfo() {
        try {
            List<TbGoods> goodsList = goodsService.getGoodsInfo();
            List<CategoryVO> goodVOList = new ArrayList<>();

            for (TbGoods tbGoods : goodsList) {
                CategoryVO goodsVO = new CategoryVO();
                BeanUtils.copyProperties(tbGoods,goodsVO);

                Long category1Id = tbGoods.getCategory1Id();
                TbItemCat infoById1 = itemCatService.getInfoById(category1Id);
                goodsVO.setCategory1(infoById1.getName());

                Long category2Id = tbGoods.getCategory2Id();
                TbItemCat infoById2 = itemCatService.getInfoById(category2Id);
                goodsVO.setCategory2(infoById2.getName());

                Long category3Id = tbGoods.getCategory3Id();
                TbItemCat infoById3 = itemCatService.getInfoById(category3Id);
                goodsVO.setCategory3(infoById3.getName());

                goodVOList.add(goodsVO);
            }
            return AjaxResult.OK().setData(goodVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }


    //批量删除goods,goods_desc,item
    @RequestMapping("deleteBatch")
    @ResponseBody
    public AjaxResult deleteBatch(@RequestParam("ids[]")Long[] ids){
        try {
            int i = goodsService.deleteBatch(ids);
            int n = itemService.deleteBatch(ids);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //批量审核
    @RequestMapping("updateBatch")
    @ResponseBody
    public AjaxResult updateBatch(@RequestParam("ids[]") Long[] ids,String auditStatus){
        try {
            int i = goodsService.updateBatch(ids,auditStatus);
            return AjaxResult.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
